package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WxMater;
import com.ruoyi.system.service.IWxMaterService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static java.util.stream.Collectors.toList;

/**
 * materController
 *
 * @author ruoyi
 * @date 2021-07-31
 */
@Controller
@RequestMapping("/system/mater")
public class WxMaterController extends BaseController {
    private String prefix = "system/mater";

    @Autowired
    private IWxMaterService wxMaterService;

    @RequiresPermissions("system:mater:view")
    @GetMapping()
    public String mater() {
        return prefix + "/mater";
    }

    /**
     * 查询mater列表
     */
    @RequiresPermissions("system:mater:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WxMater wxMater) {

        Map<Object, Object> resMap = new HashMap<>();
        SysUser currentUser = ShiroUtils.getSysUser();
        if(currentUser.getLoginName().equals("admin") || currentUser.getDeptId() == 101){
            List<SysDept> sysDepts = wxMaterService.selectDeptsByMaterPhone("");
            if(StringUtils.isNotEmpty(sysDepts)){
                List<Long> deptList = new ArrayList<>();
                for (SysDept dept : sysDepts){
                    deptList.add(dept.getDeptId());
                }
                wxMater.setDepts(deptList);
            }
            resMap.put("materName", wxMater.getMaterName());
            resMap.put("materPhone", wxMater.getMaterPhone());
            resMap.put("depts", wxMater.getDepts());

        }else{
            String phonenumber = currentUser.getPhonenumber();
            // 根据用户手机号，获取项目权限
            List<SysDept> sysDepts = wxMaterService.selectDeptsByMaterPhone(phonenumber);
            if(StringUtils.isNotEmpty(sysDepts)){
                List<Long> deptList = new ArrayList<>();
                for (SysDept dept : sysDepts){
                    deptList.add(dept.getDeptId());
                }
                wxMater.setDepts(deptList);
            }

            resMap.put("materName", wxMater.getMaterName());
            resMap.put("materPhone", wxMater.getMaterPhone());
            resMap.put("depts", wxMater.getDepts());
        }
        startPage();
        List<WxMater> list = wxMaterService.selectWxMaterLists(resMap);
        return getDataTable(list);
    }

    /**
     * 导出mater列表
     */
    @RequiresPermissions("system:mater:export")
    @Log(title = "mater", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WxMater wxMater) {
        SysUser currentUser = ShiroUtils.getSysUser();
        String phonenumber = currentUser.getPhonenumber();
        // 根据用户手机号，获取项目权限
        List<SysDept> sysDepts = wxMaterService.selectDeptsByMaterPhone(phonenumber);
        if(StringUtils.isNotEmpty(sysDepts)){
            List<Long> deptList = new ArrayList<>();
            for (SysDept dept : sysDepts){
                deptList.add(dept.getDeptId());
            }
            wxMater.setDepts(deptList);
        }
        Map<Object, Object> resMap = new HashMap<>();
        resMap.put("materName", wxMater.getMaterName());
        resMap.put("materPhone", wxMater.getMaterPhone());
        List<WxMater> list = wxMaterService.selectWxMaterLists(resMap);
        ExcelUtil<WxMater> util = new ExcelUtil<WxMater>(WxMater.class);
        return util.exportExcel(list, "mater数据");
    }

    /**
     * 新增mater
     */
    @GetMapping("/add")
    public String add(ModelMap mmp) {
        SysUser currentUser = ShiroUtils.getSysUser();
        if(currentUser.getLoginName().equals("admin") || currentUser.getDeptId() == 101){
            mmp.put("sysDepts", wxMaterService.selectDeptsByMaterPhone(""));
        }else{
            // 根据用户手机号，获取项目权限
            mmp.put("sysDepts", wxMaterService.selectDeptsByMaterPhone( ShiroUtils.getSysUser().getPhonenumber()));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存mater
     */
    @RequiresPermissions("system:mater:add")
    @Log(title = "mater", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WxMater wxMater) {
        return toAjax(wxMaterService.insertWxMater(wxMater));
    }

    /**
     * 修改mater
     */
    @GetMapping("/edit/{materId}")
    public String edit(@PathVariable("materId") Long materId, ModelMap mmap) {
        WxMater wxMater = wxMaterService.selectWxMaterById(materId);
        mmap.put("wxMater", wxMater);
        return prefix + "/edit";
    }

    /**
     * 修改保存mater
     */
    @RequiresPermissions("system:mater:edit")
    @Log(title = "mater", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WxMater wxMater) {
        return toAjax(wxMaterService.updateWxMater(wxMater));
    }

    /**
     * 删除mater
     */
    @RequiresPermissions("system:mater:remove")
    @Log(title = "mater", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wxMaterService.deleteWxMaterByIds(ids));
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(WxMater wxMater) {
        return wxMaterService.checkPhoneUnique(wxMater);
    }

    /**
     * 跳转分配权限页面
     */
    @GetMapping("/assign/{materId}")
    public String assign(@PathVariable("materId") Long materId, ModelMap mmap) {
        // 全列表 -> 获取该材料员所属工程公司下其他的项目列表
        List<SysDept> sysDepts = wxMaterService.selectDeptsByMaterId(materId);

        // 选中列表
        List<SysDept> selDepts = wxMaterService.selectAssDeptsByMaterId(materId);


        for(SysDept sysDept : selDepts){
            for(SysDept s :sysDepts){
                if(sysDept.getDeptId().equals(s.getDeptId()) ){
                    s.setSelFlag(true);
                }
            }
        }

        mmap.put("sysDepts", sysDepts);
        mmap.put("materId", materId);
        return prefix + "/assign";
    }

    /**
     * 新增保存mater
     */
    @RequiresPermissions("system:mater:assign")
    @PostMapping("/saveAssProgram")
    @ResponseBody
    public AjaxResult saveAssProgram(WxMater wxMater) {
        wxMaterService.deleteProPermissionByMaterId(wxMater.getMaterId());

        return toAjax(wxMaterService.saveAssProgram(wxMater));
    }
}
